package com.jiayang.hhotel.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.presenter.AddedContractPresenter;
import com.jiayang.hhotel.util.CommonUtil;
import com.jiayang.hhotel.util.DateUtil;
import com.jiayang.hhotel.util.LogUtil;
import com.jiayang.hhotel.util.StringUtil;
import com.jiayang.hhotel.util.ToastUtil;

import java.io.FileNotFoundException;

import cn.droidlover.xdroidmvp.mvp.XActivity;

/**
 * Created by Administrator on 2017/5/26 0026.
 * 新增合同页面
 */

public class AddedContractActivity extends XActivity<AddedContractPresenter> implements View.OnClickListener {

    private EditText num,//合同编号
                    describe,//描述
                    department,//部门选择
                    synopsis;//简介

    private TextView contactPersonSel,//联系人选择
                    startDate,//起始日期区间选择
                    endDate,//终止日期区间选择
                    team,//团会公付签单
                    Individual;//散客公付签单

    private ImageView picSel;//图片选择
    private Button save;//保存

    @Override
    public void initData(Bundle savedInstanceState) {
        initTitle(this,true,getString(R.string.newly_added),R.color.whitetext,null,null);

        num= (EditText) findViewById(R.id.et_num);
        describe= (EditText) findViewById(R.id.et_describe);
        department= (EditText) findViewById(R.id.et_department_select);
        synopsis= (EditText) findViewById(R.id.et_synopsis);

        contactPersonSel= (TextView) findViewById(R.id.bt_contact_person_sel);
        startDate= (TextView) findViewById(R.id.bt_start_date_section);
        endDate= (TextView) findViewById(R.id.bt_end_date_section);
        team= (TextView) findViewById(R.id.bt_team);
        Individual= (TextView) findViewById(R.id.bt_Individual);
        picSel= (ImageView) findViewById(R.id.iv_pic);
        save= (Button) findViewById(R.id.bt_save);

        contactPersonSel.setOnClickListener(this);
        startDate.setOnClickListener(this);
        endDate.setOnClickListener(this);
        team.setOnClickListener(this);
        Individual.setOnClickListener(this);
        picSel.setOnClickListener(this);
        save.setOnClickListener(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_added_contract;
    }

    @Override
    public AddedContractPresenter newP() {
        return new AddedContractPresenter();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_contact_person_sel://联系人选择
                break;
            case R.id.bt_start_date_section://起始日期区间选择
                break;
            case R.id.bt_end_date_section://终止日期区间选择
                break;
            case R.id.bt_team://团会公付签单
                break;
            case R.id.bt_Individual://散客公付签单
                break;
            case R.id.iv_pic://图片选择
                getP().createSelPictureDialog(this);
                break;
            case R.id.bt_save://保存
                String numStr=num.getText().toString();
                String describeStr=describe.getText().toString();
                String departmentStr=department.getText().toString();
                String contactPersonSelStr=contactPersonSel.getText().toString();
                String startDateStr=startDate.getText().toString();
                String endDateStr=endDate.getText().toString();
                String teamStr=team.getText().toString();
                String IndividualStr=Individual.getText().toString();
                String synopsisStr=synopsis.getText().toString();
                if(StringUtil.isEmpty(numStr)){
                    ToastUtil.Toast(getString(R.string.please_input_contract_num),this);
                }else if(StringUtil.isEmpty(describeStr)){
                    ToastUtil.Toast(getString(R.string.please_input_your_describe),this);
                }else if(StringUtil.isEmpty(departmentStr)){
                    ToastUtil.Toast(getString(R.string.please_input_your_english_describe),this);
                }else if(contactPersonSelStr.equals(getString(R.string.click_select))){
                    ToastUtil.Toast(getString(R.string.please_sel_contact_person),this);
                }else if(startDateStr.equals(getString(R.string.click_select))){
                    ToastUtil.Toast(getString(R.string.please_sel_start_date_section),this);
                }else if(endDateStr.equals(getString(R.string.click_select))){
                    ToastUtil.Toast(getString(R.string.please_sel_end_date_section),this);
                }else if(teamStr.equals(getString(R.string.click_select))){
                    ToastUtil.Toast(getString(R.string.please_sel_The_public_will_pay_the_bill),this);
                }else if(IndividualStr.equals(getString(R.string.click_select))){
                    ToastUtil.Toast(getString(R.string.please_sel_Individual_bills_payable),this);
                }else if(!StringUtil.isEmpty(synopsisStr)){
                    ToastUtil.Toast(getString(R.string.please_input_your_synopsis),this);
                }else{
                    //todo 上传内容
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case Constant.REQUEST_CODE_START_CAMERA://相机
                if(resultCode==RESULT_OK){
                    try {
                       /**
                         * 该uri就是照片文件夹对应的uri
                        */
                       if(getP().getImageUri()!=null){
                           Bitmap bit = BitmapFactory.decodeStream(getContentResolver().openInputStream(getP().getImageUri()));
                           picSel.setImageBitmap(bit);
                       }else{
                           ToastUtil.Toast("未知错误",this);
                       }
                    } catch (Exception e) {
                        ToastUtil.Toast("未知错误Exception",this);
                        e.printStackTrace();
                    }
                }
                break;
            case Constant.REQUEST_CODE_START_ALBUM://相册返回
                if(resultCode==RESULT_OK){
                    Uri uri=data.getData();
                    try {
                        if(uri!=null){
                            Bitmap bit = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
                            picSel.setImageBitmap(bit);
                            LogUtil.showLog(uri.getEncodedPath());
//                            CommonUtil.savePhotoToSDCard(CommonUtil.getImagePath(), DateUtil.getCurrentMillionTime()+"","jpg",bit);
                        }
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                break;
        }
    }
}
